package com.Web_02;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
@WebServlet(name = "RequestDemo3", value = "/RequestDemo3")
public class RequestDemo3 extends HttpServlet {
//          获取请求体
//          请求体格式：  username=张三&password=123
    /*
    String getParameter(String name)            根据参数名称获取参数值
    String[] getParameterValues(String name)    根据参数名称获取参数值的数组
    Enumeration getParameterNames()             获取所有请求参数参数的名称--->称为枚举，一般不用
    Map<String,string[]>getParameterMap()       获取所有请求参数的map集合
     */

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("手动获取");
//        起的名字需要和html中的名字一致
        String username=request.getParameter("username");
        System.out.println("用户："+username);
        String password = request.getParameter("password");
        System.out.println("密码"+password);

        String[] hobby = request.getParameterValues("hobby");
//        hobby是数组的形式，需要将其转化为字符串的形式并打印
        System.out.println("爱好"+ Arrays.toString(hobby));
        System.out.println("自动获取------");
        Map<String,String[]>parameterMap=request.getParameterMap();
//        k代表String v代表String[]
        parameterMap.forEach((k,v)-> System.out.println(k+"="+Arrays.toString(v)));

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        指定post的解码方式
        request.setCharacterEncoding("UTF-8");
        String name=request.getParameter("name");
        String password=request.getParameter("password");
        System.out.println("用户名："+name);
        System.out.println("密码："+password);
    }
}
